<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionController::Assertions::ResponseAssertions</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionController::Assertions::ResponseAssertions</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/actionpack/lib/action_controller/assertions/response_assertions_rb.html">
                vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
A small suite of assertions that test responses from <a
href="../../Rails.html">Rails</a> applications.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000989">assert_redirected_to</a>&nbsp;&nbsp;
      <a href="#M000988">assert_response</a>&nbsp;&nbsp;
      <a href="#M000990">assert_template</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000989" class="method-detail">
        <a name="M000989"></a>

        <div class="method-heading">
          <a href="#M000989" class="method-signature">
          <span class="method-name">assert_redirected_to</span><span class="method-args">(options = {}, message=nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Assert that the redirection options passed in match those of the redirect
called in the latest action. This match can be partial, such that <a
href="ResponseAssertions.html#M000989">assert_redirected_to</a>(:controller
=&gt; &quot;weblog&quot;) will also match the redirection of
redirect_to(:controller =&gt; &quot;weblog&quot;, :action =&gt;
&quot;show&quot;) and so on.
</p>
<h4>Examples</h4>
<pre>
  # assert that the redirection was to the &quot;index&quot; action on the WeblogController
  assert_redirected_to :controller =&gt; &quot;weblog&quot;, :action =&gt; &quot;index&quot;

  # assert that the redirection was to the named route login_url
  assert_redirected_to login_url

  # assert that the redirection was to the url for @customer
  assert_redirected_to @customer
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000989-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000989-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 62</span>
62:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_redirected_to</span>(<span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">message</span>=<span class="ruby-keyword kw">nil</span>)
63:         <span class="ruby-identifier">clean_backtrace</span> <span class="ruby-keyword kw">do</span>
64:           <span class="ruby-identifier">assert_response</span>(<span class="ruby-identifier">:redirect</span>, <span class="ruby-identifier">message</span>)
65:           <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">redirected_to</span>
66:           
67:           <span class="ruby-comment cmt"># Support partial arguments for hash redirections</span>
68:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">redirected_to</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>)
69:             <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">all?</span> {<span class="ruby-operator">|</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>)<span class="ruby-operator">|</span> <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">redirected_to</span>[<span class="ruby-identifier">key</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">value</span>}
70:           <span class="ruby-keyword kw">end</span>
71:           
72:           <span class="ruby-identifier">redirected_to_after_normalisation</span> = <span class="ruby-identifier">normalize_argument_to_redirection</span>(<span class="ruby-ivar">@response</span>.<span class="ruby-identifier">redirected_to</span>)
73:           <span class="ruby-identifier">options_after_normalisation</span>       = <span class="ruby-identifier">normalize_argument_to_redirection</span>(<span class="ruby-identifier">options</span>)
74: 
75:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">redirected_to_after_normalisation</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">options_after_normalisation</span>
76:             <span class="ruby-identifier">flunk</span> <span class="ruby-node">&quot;Expected response to be a redirect to &lt;#{options_after_normalisation}&gt; but was a redirect to &lt;#{redirected_to_after_normalisation}&gt;&quot;</span>
77:           <span class="ruby-keyword kw">end</span>
78:         <span class="ruby-keyword kw">end</span>
79:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000988" class="method-detail">
        <a name="M000988"></a>

        <div class="method-heading">
          <a href="#M000988" class="method-signature">
          <span class="method-name">assert_response</span><span class="method-args">(type, message = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Asserts that the response is one of the following types:
</p>
<ul>
<li><tt>:success</tt> - Status code was 200

</li>
<li><tt>:redirect</tt> - Status code was in the 300-399 range

</li>
<li><tt>:missing</tt> - Status code was 404

</li>
<li><tt>:error</tt> - Status code was in the 500-599 range

</li>
</ul>
<p>
You can also pass an explicit status number like <a
href="ResponseAssertions.html#M000988">assert_response</a>(501) or its
symbolic equivalent <a
href="ResponseAssertions.html#M000988">assert_response</a>(:not_implemented).
See ActionController::StatusCodes for a full list.
</p>
<h4>Examples</h4>
<pre>
  # assert that the response was a redirection
  assert_response :redirect

  # assert that the response code was status code 401 (unauthorized)
  assert_response 401
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000988-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000988-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 27</span>
27:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_response</span>(<span class="ruby-identifier">type</span>, <span class="ruby-identifier">message</span> = <span class="ruby-keyword kw">nil</span>)
28:         <span class="ruby-identifier">clean_backtrace</span> <span class="ruby-keyword kw">do</span>
29:           <span class="ruby-keyword kw">if</span> [ <span class="ruby-identifier">:success</span>, <span class="ruby-identifier">:missing</span>, <span class="ruby-identifier">:redirect</span>, <span class="ruby-identifier">:error</span> ].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">type</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{type}?&quot;</span>)
30:             <span class="ruby-identifier">assert_block</span>(<span class="ruby-value str">&quot;&quot;</span>) { <span class="ruby-keyword kw">true</span> } <span class="ruby-comment cmt"># to count the assertion</span>
31:           <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Fixnum</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">response_code</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">type</span>
32:             <span class="ruby-identifier">assert_block</span>(<span class="ruby-value str">&quot;&quot;</span>) { <span class="ruby-keyword kw">true</span> } <span class="ruby-comment cmt"># to count the assertion</span>
33:           <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">response_code</span> <span class="ruby-operator">==</span> <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">StatusCodes</span><span class="ruby-operator">::</span><span class="ruby-constant">SYMBOL_TO_STATUS_CODE</span>[<span class="ruby-identifier">type</span>]
34:             <span class="ruby-identifier">assert_block</span>(<span class="ruby-value str">&quot;&quot;</span>) { <span class="ruby-keyword kw">true</span> } <span class="ruby-comment cmt"># to count the assertion</span>
35:           <span class="ruby-keyword kw">else</span>
36:             <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">error?</span>
37:               <span class="ruby-identifier">exception</span> = <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">template</span>.<span class="ruby-identifier">instance_variable_get</span>(<span class="ruby-identifier">:@exception</span>)
38:               <span class="ruby-identifier">exception_message</span> = <span class="ruby-identifier">exception</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">exception</span>.<span class="ruby-identifier">message</span>
39:               <span class="ruby-identifier">assert_block</span>(<span class="ruby-identifier">build_message</span>(<span class="ruby-identifier">message</span>, <span class="ruby-value str">&quot;Expected response to be a &lt;?&gt;, but was &lt;?&gt;\n&lt;?&gt;&quot;</span>, <span class="ruby-identifier">type</span>, <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">response_code</span>, <span class="ruby-identifier">exception_message</span>.<span class="ruby-identifier">to_s</span>)) { <span class="ruby-keyword kw">false</span> }
40:             <span class="ruby-keyword kw">else</span>
41:               <span class="ruby-identifier">assert_block</span>(<span class="ruby-identifier">build_message</span>(<span class="ruby-identifier">message</span>, <span class="ruby-value str">&quot;Expected response to be a &lt;?&gt;, but was &lt;?&gt;&quot;</span>, <span class="ruby-identifier">type</span>, <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">response_code</span>)) { <span class="ruby-keyword kw">false</span> }
42:             <span class="ruby-keyword kw">end</span>
43:           <span class="ruby-keyword kw">end</span>
44:         <span class="ruby-keyword kw">end</span>
45:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000990" class="method-detail">
        <a name="M000990"></a>

        <div class="method-heading">
          <a href="#M000990" class="method-signature">
          <span class="method-name">assert_template</span><span class="method-args">(expected = nil, message=nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Asserts that the request was rendered with the appropriate template file.
</p>
<h4>Examples</h4>
<pre>
  # assert that the &quot;new&quot; view template was rendered
  assert_template &quot;new&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000990-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000990-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 88</span>
 88:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_template</span>(<span class="ruby-identifier">expected</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">message</span>=<span class="ruby-keyword kw">nil</span>)
 89:         <span class="ruby-identifier">clean_backtrace</span> <span class="ruby-keyword kw">do</span>
 90:           <span class="ruby-identifier">rendered</span> = <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">rendered_template</span>.<span class="ruby-identifier">to_s</span>
 91:           <span class="ruby-identifier">msg</span> = <span class="ruby-identifier">build_message</span>(<span class="ruby-identifier">message</span>, <span class="ruby-value str">&quot;expecting &lt;?&gt; but rendering with &lt;?&gt;&quot;</span>, <span class="ruby-identifier">expected</span>, <span class="ruby-identifier">rendered</span>)
 92:           <span class="ruby-identifier">assert_block</span>(<span class="ruby-identifier">msg</span>) <span class="ruby-keyword kw">do</span>
 93:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">expected</span>.<span class="ruby-identifier">nil?</span>
 94:               <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">rendered_template</span>.<span class="ruby-identifier">blank?</span>
 95:             <span class="ruby-keyword kw">else</span>
 96:               <span class="ruby-identifier">rendered</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">match</span>(<span class="ruby-identifier">expected</span>)
 97:             <span class="ruby-keyword kw">end</span>
 98:           <span class="ruby-keyword kw">end</span>
 99:         <span class="ruby-keyword kw">end</span>
100:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>